*===============================================================================
*
*					WORKER BELIEFS ABOUT OUTSIDE OPTIONS
*		(c)	Simon Jaeger, Christopher Roth, Nina Roussille, Benjamin Schoefer
*							  2023 December 5
*						   	   SOEP-IAB Data 
*
*===============================================================================


********************************************************************************
*								Figure V	 								   *
********************************************************************************

cap log close
log using ${log}/FigureVab.log, replace     
 
* SOEP datasets
use "$data/GSOEPIS2019_survey_long.dta", clear 
append using "$data/GSOEPIS2020_survey_long.dta"

gen jahr = syear
drop syear

* narrowing down to relevant analysis sample
merge m:1 pid jahr using "$data/unionsample_finaldata.dta", keepusing(pid)
keep if _merge==3
drop _merge

* adding firm IDs (link to firm AKM effects)
merge 1:m pid jahr using ${data}/data_prep_ADIAB_pid_betnr_jahr.dta
drop if _merge == 2
drop _merge

* adding true rank within occupation - this file is created by 4_occupationrank.do
merge m:1 pid using "$data/within_occupation_rank_2019.dta"
drop if _merge==2
drop _merge

* keep individuals with nonmissing subjective and objective rank
keep if salary_less_proba!=.&salary_more_proba!=.&ieb_beruf_kons_num_ventile!=.

* generate occupation percentile rank
gen occupation_pctl_belief = (salary_less_proba + 100-salary_more_proba)/2
gen occupation_pctl_true = ieb_beruf_kons_num_ventile*5 - 2 // -2 to shift distribution and aid comparison in histogram 

* distribution: share of obs below 0.1, 0.4, 0.6, 0.9 
foreach dist in belief true{
	su occupation_pctl_`dist' 
		local N_all = r(N) 
	forvalues i=1/9{
		su occupation_pctl_`dist' if occupation_pctl_`dist' <= `i'*10
			local N_perc = r(N) 
		local perc = `N_perc' / `N_all'
		di "`dist' `i': `perc'"
	}
}


*****************************
* Figure 5 a 
*****************************

sum occupation_pctl_belief, detail
local N_obs_beliefs = r(N)
sum occupation_pctl_true, detail
local N_obs_data = r(N)

twoway (hist occupation_pctl_belief,  frac width(5) color(navy%60) fcolor(navy)) (hist occupation_pctl_true,  frac width(5) color(maroon%20) fcolor(maroon) ),  ///
	xtitle("Within-Occupation Wage Rank Percentile") graphregion(color(white)) legend(order(1 "Beliefs, N = `N_obs_beliefs'" 2 "Benchmark (Admin. Data), N = `N_obs_data'") cols(1) region(lwidth(none))) ///
	ylabel(0 "0" 0.05 "0.05" 0.1 "0.1" 0.15 "0.15" 0.2 "0.2" 0.25 "0.25")
graph export ${log}/FigureVa.pdf, replace
    



*****************************
* Figure 5 b
*****************************

*** binscatter of objective on subjective within-occupation rank  
reg occupation_pctl_belief occupation_pctl_true, cluster(pid)
	local N_obs = e(N)
	local beta = string(round(_b[occupation_pctl_true],0.001),"%4.3f")
	local se = string(round(_se[occupation_pctl_true],0.001),"%4.3f")
	
binscatter occupation_pctl_belief occupation_pctl_true occupation_pctl_true, ///
	msymbols(circle none) xtitle("Rank in Occupation (Objective)") ytitle("Rank in Occupation (Belief)") lcolors(maroon gray) ///
	legend(on order(- "Slope: `beta' (SE `se')") region(lwidth(none)) size(medsmall) pos(11) ring(0)) /// 
	xscale(range(0 100)) yscale(range(0 100)) ylabel(#10,grid) xlabel(#10,grid)
	
graph export ${log}/FigureVb.pdf, replace

   
log close
clear
